package info.loveyouonly.renren.login;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

public class RP {

	public static void main(String[] args) {
		Properties prop = new Properties();
		try {
			prop.load(new FileInputStream("config.properties"));
			RP rp = new RP(prop.getProperty("userName"),
					prop.getProperty("password"));
			rp.startRefresh();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	private String userName;
	private String password;

	public RP(String userName, String password) {
		this.userName = userName;
		this.password = password;
	}

	public void startRefresh() {
		int frequency = 1800;
		while (true) {
			try {
				System.out.println("your current RP value is: " + getRP());
				Thread.sleep(frequency * 1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public int getRP() {
		try {
			String beginPattern = "bTotalRpNum\">";
			RenRen renrenclient = new RenRen();
			renrenclient.setUserName(userName);
			renrenclient.setPassword(password);
			String result = renrenclient.getReplyAsText();
			String endPattern = "</b>";
			int beginIndex = result.indexOf(beginPattern);
			result = result.substring(beginIndex);
			int endIndex = result.indexOf(endPattern);
			result = result.substring(0, endIndex);
			result = result.replace(beginPattern, "");
			return Integer.parseInt(result);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return Integer.MIN_VALUE;
	}
}
